package com.markby.fscm.cryptogram.uitls;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class HexUtils {
    protected final static String[] hexChars = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
    /**
     * Hexify a byte array.
     *
     * @param data
     *            Byte array to convert to HexString
     *
     * @return HexString
     */
    @SuppressWarnings("DuplicatedCode")
    public static String hexify_haveSpace(byte[] data) {
        if (data == null) {
            return "null";
        }

        StringBuilder out = new StringBuilder (1000);
        int n = 0;

        for (byte datum : data) {
            if (n == 0) {
                out.append ( ' ' );
            }
            out.append ( hexChars[(datum >> 4) & 0x0f] );
            out.append ( hexChars[datum & 0x0f] );
        }
        return out.toString();
    }

    /**
     * 把int转换成byte[4]
     *
     * @return returnData
     */
    public static byte[] intToBytes(int x, ByteOrder byteOrder) {
        ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
        buffer.order(byteOrder);
        buffer.putInt(x);
        return buffer.array();
    }

}
